﻿using RsCms.Application.Posts.Dto;
using RsCms.Core;

using Mapster;
using RsCode;
using RsCms.Core.Tags;
namespace RsCms.Application.Posts
{
    public class PostsAppService:IPostsAppService
    {
        IPostsRepository postRepository;
        IPostDomainService postDomainService;
        ITagsRepository tagsRepository;
        public PostsAppService(IPostDomainService postDomainService, IPostsRepository postRepository,ITagsRepository  tagsRepository)
        {
            this.postRepository= postRepository;
            this.postDomainService= postDomainService;
            this.tagsRepository= tagsRepository;
        }

        public async Task<PostsDto> GetPostsAsync(string id)
        {
            var info=await postDomainService.GetPostsAsync(id);
            var target= info?.Adapt<Dto.PostsDto>()??new PostsDto();
             HtmlDecode(target);
            return target;
        }
        public async Task<List<PostsListDto>> QueryPostsAsync()
        {
            var info = await postDomainService.PagePublishAsync(1, 20);
            return info.Items.Adapt<List<PostsListDto>>();
        }

        public async Task<PageData<PostsDto>>PagePublishAsync(int page,int pageSize, string date=null)
        {
            var dt=date!=null? date.Split(','):null;
            var info=await postDomainService.PagePublishAsync(page,pageSize,dt);
            return info.Adapt<PageData<PostsDto>>();
        }

        public async Task<PostsDto> GetPostsBySlugAsync(string slug)
        {
            var info = await postDomainService.GetPostsBySlugAsync(slug);
            var target= info?.Adapt<Dto.PostsDto>() ?? new PostsDto();
            HtmlDecode(target);
            return target;
        }

        public async Task<PostsDto> SaveAsync(PostEditDto dto)
        {
            HtmlEncode(dto);
            var posts = dto.Adapt<Entities.Posts>();

            posts.Type = "post";
            var info=await postDomainService.SaveAsync(posts);
            var target= info.Adapt<PostsDto>();
            HtmlDecode(target);
            return target;
        }

        void HtmlEncode(PostEditDto posts)
        {
            if (!string.IsNullOrWhiteSpace(posts.Title))
                posts.Title = System.Web.HttpUtility.HtmlEncode(posts.Title);
            if (!string.IsNullOrWhiteSpace(posts.Markdown))
                posts.Markdown = System.Web.HttpUtility.HtmlEncode(posts.Markdown);
        }
        void HtmlDecode(PostsDto posts)
        {
            if(!string.IsNullOrWhiteSpace( posts.Title))
                posts.Title = System.Web.HttpUtility.HtmlDecode(posts.Title);
            if (!string.IsNullOrWhiteSpace(posts.Markdown))
                posts.Markdown = System.Web.HttpUtility.HtmlDecode(posts.Markdown);
        }
        void HtmlDecode(PostEditDto posts)
        {
            if (!string.IsNullOrWhiteSpace(posts.Title))
                posts.Title = System.Web.HttpUtility.HtmlDecode(posts.Title);
            if (!string.IsNullOrWhiteSpace(posts.Markdown))
                posts.Markdown = System.Web.HttpUtility.HtmlDecode(posts.Markdown);
        }

        public async Task<PageData<PostsDto>>PageDraftsAsync(int page,int pageSize, string date=null)
        {
            var dt = date != null ? date.Split(',') : null;
            var info = await postDomainService.PageDraftsAsync(page, pageSize, dt);
            return info.Adapt<PageData<PostsDto>>();
        }

        public async Task<PageData<PostsDto>> PageScheduledAsync(int page, int pageSize,string date=null)
        {
            var dt = date != null ? date.Split(',') : null;
            var info = await postDomainService.PageScheduledAsync(page, pageSize, dt);
            return info.Adapt<PageData<PostsDto>>();
        }

        public Task DeleteAsync(string id)
        {
            postRepository.PostStore.Delete(x=>x.Id == id);
            return Task.CompletedTask;
        }

        public async Task<PageData<PostsDto>> PageAsync(int page,int pageSize)
        {
            var info = await postDomainService.PageAsync(page, pageSize);
            return info.Adapt<PageData<PostsDto>>();
        }

        public async Task<PageData<PostsDto>> PageAsync(int page, int pageSize,string tag)
        {
            var tagInfo=await tagsRepository.Select.Where(x=>x.Name == tag).ToOneAsync();
            if (tagInfo == null)
                return new PageData<PostsDto> {  Items=new List<PostsDto>()};
            var tagId=tagInfo.Id; 
            var info = await postDomainService.PageAsync(page, pageSize,tagId);
            return info.Adapt<PageData<PostsDto>>();
        }


        public async Task<PostMetaDto> GetPostMetaAsync(string postId)
        {
            var info = await GetPostsAsync(postId);
            var metaInfo=await postRepository.PostMetaStore.Select.Where(x=>x.PostId== postId).ToOneAsync();
             
            if(metaInfo!=null)
            {
                return metaInfo.Adapt<PostMetaDto>();
            }else
            {
                return new PostMetaDto();
            }
        }

        public async Task<PostMetaDto>SavePostMetaAsync(PostMetaDto dto)
        {
            var info=dto.Adapt<Entities.PostsMeta>();
            info= await postDomainService.SaveAsync(info);
            return info.Adapt<PostMetaDto>();
        }
    }
}
